<html>
<head><meta charset="utf-8"><title>hir_def ast::Item::Module · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html">hir_def ast::Item::Module</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219596298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219596298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219596298">(Dec 11 2020 at 11:54)</a>:</h4>
<p>In this line, I read it as a Module does not have inner items, can someone explain why that is the case?</p>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/item_tree/lower.rs#L100">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/item_tree/lower.rs#L100</a></p>



<a name="219596409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219596409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219596409">(Dec 11 2020 at 11:55)</a>:</h4>
<p>Contrasting, ast::Item::Func does have inner items</p>



<a name="219597442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219597442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219597442">(Dec 11 2020 at 12:06)</a>:</h4>
<p>Good question <span class="user-mention" data-user-id="369769">@kev</span> !</p>
<p>I think the primary reason is historical -- items nested in fn's and module items are treeted differently higher in the stack (CrateDefMap)</p>



<a name="219597483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219597483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219597483">(Dec 11 2020 at 12:07)</a>:</h4>
<p>But I guess on the ItemTree level it might make sense to unify the representation? <span class="user-mention" data-user-id="211727">@Jonas Schievink</span> , do you think we can maybe use some smart encoding here?</p>



<a name="219597505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219597505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219597505">(Dec 11 2020 at 12:07)</a>:</h4>
<p>Like, flattening <em>all</em> items in a single array, and using some indexing scheme to infer the tree structure?</p>



<a name="219598092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598092">(Dec 11 2020 at 12:14)</a>:</h4>
<p>hmm, maybe</p>



<a name="219598123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598123">(Dec 11 2020 at 12:14)</a>:</h4>
<p>Not really sure how that would look, I'm not very good with algorithms :)</p>



<a name="219598143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598143">(Dec 11 2020 at 12:14)</a>:</h4>
<p>I guess you're imagining something like how you can store a binary heap in a linear array, and use indices to navigate it?</p>



<a name="219598161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598161">(Dec 11 2020 at 12:15)</a>:</h4>
<p>Sortof, yeah</p>



<a name="219598195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598195">(Dec 11 2020 at 12:15)</a>:</h4>
<p>The encoding would have to be more complicated, as that's n-ary tree</p>



<a name="219598276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598276">(Dec 11 2020 at 12:16)</a>:</h4>
<p>But something like this would contain all the requred information:</p>
<div class="codehilite"><pre><span></span><code>item_tree: Vec&lt;(usize, Item)&gt;
</code></pre></div>
<p>where <code>usize</code> is the index of parent</p>



<a name="219598494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598494">(Dec 11 2020 at 12:19)</a>:</h4>
<p>An alternative would be <code>Vec&lt;(Item, ops::Range&lt;usize&gt;)&gt;</code>, where the range specifies the children</p>



<a name="219598617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598617">(Dec 11 2020 at 12:20)</a>:</h4>
<p>Do we need to update those (e.g. to add children)?</p>



<a name="219598698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219598698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219598698">(Dec 11 2020 at 12:21)</a>:</h4>
<p>Not really, we build the thing once</p>



<a name="219600328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219600328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219600328">(Dec 11 2020 at 12:37)</a>:</h4>
<p>Ahh I see, thank you for explaining @matklad :) Peeking into CrateDefMap now</p>



<a name="219600749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/hir_def%20ast%3A%3AItem%3A%3AModule/near/219600749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/hir_def.20ast.3A.3AItem.3A.3AModule.html#219600749">(Dec 11 2020 at 12:41)</a>:</h4>
<p>Hmm oh I think I understand fully, items inside of a function cannot be called from outside of that scope, but items in mod can, so I guess you needed a way to represent this distinction. So modules have their own Scope Variant</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>